package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.files.entity.Files;
import cn.com.self.apis.member.entity.UFileRelation;
import cn.com.self.apis.member.mapper.ReportFileMapper;
import cn.com.self.apis.portal.entity.UDisputeEntity;
import cn.com.self.apis.portal.mapper.DisputeMapper;
import cn.com.self.apis.portal.service.intf.IDisputeService;
import cn.com.self.apis.portal.util.ConstantUtils;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class DisputeServiceImpl extends BaseServiceImpl<UDisputeEntity, String> implements IDisputeService {

    @Autowired
    private DisputeMapper disputeMapper;
    @Autowired
    private ReportFileMapper reportFileMapper;
    @Override
    public UDisputeEntity createDispute(UDisputeEntity entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDisputeState(entity.getDisputeState() == null ? 1 : entity.getDisputeState());
        UDisputeEntity uDisputeEntity = this.create(entity);
        ArrayList<Files> disputeFiles = entity.getDisputeFiles();
        if(disputeFiles != null){
            for(Files disputeFile : disputeFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(disputeFile.getId());
                uFileRelation.setObjId(uDisputeEntity.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_JIUFEN);
                reportFileMapper.insert(uFileRelation);
            }
        }
        return uDisputeEntity;
    }

    @Override
    public UDisputeEntity loadDisputeInfoById(String id) {
        ArrayList<Files> disputeFiles = disputeMapper.queryDisputeFiles(id,ConstantUtils.FILE_TYPE_JIUFEN);
        UDisputeEntity disputeEntity = disputeMapper.loadDisputeInfoById(id);
        if(disputeEntity != null){
            disputeEntity.setDisputeFiles(disputeFiles);
        }
        return disputeEntity;
    }

    @Override
    public UDisputeEntity updateDispute(String id, UDisputeEntity entity) {
        disputeMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_JIUFEN);
        UDisputeEntity dispute = this.load(id);
        CopyUtils.copyProperties(entity, dispute);
        dispute.setUpdateTime(new Date());
        UDisputeEntity update = this.update(dispute);
        ArrayList<Files> disputeFiles = entity.getDisputeFiles();
        if(disputeFiles != null){
            for(Files file : disputeFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(file.getId());
                uFileRelation.setObjId(dispute.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_JIUFEN);
                reportFileMapper.insert(uFileRelation);
            }
        }

        return update;
    }

    @Override
    public void deleteUFile(String id) {
        disputeMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_JIUFEN);
    }
}
